package com.pm.security;

import com.pm.entity.User;
import com.pm.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Arrays;

/**
 * 用户详情服务实现类
 * 用于Spring Security加载用户信息
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        try {
            System.out.println("[DEBUG] loadUserByUsername called with username: " + username);
            User user = userMapper.findByUsername(username);
            if (user == null) {
                System.out.println("[DEBUG] User not found: " + username);
                throw new UsernameNotFoundException("用户不存在: " + username);
            }
            
            System.out.println("[DEBUG] User found: " + user.getUsername() + ", passwordHash: " + (user.getPasswordHash() != null ? "[PRESENT]" : "[NULL]"));
            UserPrincipal principal = new UserPrincipal(user);
            System.out.println("[DEBUG] UserPrincipal created, password: " + (principal.getPassword() != null ? "[PRESENT]" : "[NULL]"));
            return principal;
        } catch (Exception e) {
            System.out.println("[DEBUG] Exception in loadUserByUsername: " + e.getClass().getSimpleName() + ": " + e.getMessage());
            e.printStackTrace();
            throw new UsernameNotFoundException("加载用户信息失败: " + e.getMessage(), e);
        }
    }

    /**
     * 用户主体类
     */
    public static class UserPrincipal implements UserDetails {
        private User user;
        private Collection<? extends GrantedAuthority> authorities;

        public UserPrincipal(User user) {
            this.user = user;
            // 这里可以根据用户角色设置权限
            this.authorities = Arrays.asList(new SimpleGrantedAuthority("ROLE_USER"));
        }

        public User getUser() {
            return user;
        }

        @Override
        public Collection<? extends GrantedAuthority> getAuthorities() {
            return authorities;
        }

        @Override
        public String getPassword() {
            return user.getPasswordHash();
        }

        @Override
        public String getUsername() {
            return user.getUsername();
        }

        @Override
        public boolean isAccountNonExpired() {
            return true;
        }

        @Override
        public boolean isAccountNonLocked() {
            return true;
        }

        @Override
        public boolean isCredentialsNonExpired() {
            return true;
        }

        @Override
        public boolean isEnabled() {
            return true;
        }
    }
}